<?php
/**
 * Created by b17
 * 2/15/12 7:50 PM
 */

namespace Managers;

class DeliveryMessages extends BaseManager implements CrudManager
{
    const TABLE = '\Catalog\DeliveryMessages';
    protected $__table = self::TABLE;

    protected function createQueryBuilder()
    {
        $qb = $this->_em->createQueryBuilder()
            ->select('partial dm.{id, title, startTime, isDelivered}')
            ->from($this->__table, 'dm')
            ->andWhere('dm.isDel = 0');
        return $qb;
    }

    public function getList()
    {
        $qb = $this->createQueryBuilder()
            ->addOrderBy('dm.id', 'DESC');
        $res = $qb->getQuery()->getArrayResult();
        return $res;
    }

    /**
     * Создает новую запись.
     * @param array $values
     */
    public function create(array $values)
    {
        $entity = new \Catalog\DeliveryMessages();
        $this->setEntityValues($entity, $values);
        $entity->setIsDelivered(false);
        $entity->setIsDel(false);
        $this->_em->persist($entity);
        $this->flush();
        return $entity;
    }

    protected function setEntityValues(\Catalog\DeliveryMessages $entity, array $values)
    {
        $entity->setTitle($values['title']);
        $entity->setContent($values['content']);
        if (empty($values['startTime'])) {
            $entity->setStartTime(null);
        } else {
            $entity->setStartTime(\DateTime::createFromFormat(Utils::DATETIME_FORMAT, $values['startTime']));
        }
    }

    /**
     * Обновляет запись.
     * @param array $values
     */
    public function update(array $values)
    {
        $entity = $this->find($values['id']);
        $this->setEntityValues($entity, $values);
        $this->_em->persist($entity);
        $this->flush();
        return $entity;
    }

    /**
     * Возвращает елемент по ID, false - если елемент не найден.
     *
     * @param array|bool $id
     */
    public function getById($id)
    {
        $qb = $this->createQueryBuilder()
            ->select('dm')
            ->andWhere('dm.id = :id')->setParameter('id', $id);
        $res = $qb->getQuery()->getArrayResult();
        return count($res) ? $res[0] : false;
    }

    public function getUndeliveredList()
    {
        $qb = $this->createQueryBuilder()
            ->select('dm')
            ->andWhere('dm.isDelivered = 0');
        $res = $qb->getQuery()->getArrayResult();
        return $res;
    }

    public function deliverNow($id)
    {
        $entity = $this->find($id);
        if (!$entity) {
            throw new \LogicException('Record not found.');
        }
        /* @var \Catalog\DeliveryMessages $entity */
        $entity->setStartTime(new \DateTime());
        $this->_em->persist($entity);
        $this->flush();
        return $entity;
    }

    public function setDelivered($id)
    {
        $entity = $this->find($id);
        if (!$entity) {
            throw new \LogicException('Record not found.');
        }
        /* @var \Catalog\DeliveryMessages $entity */
        $entity->setIsDelivered(true);
        $this->_em->persist($entity);
        $this->flush();
        return $entity;
    }
}
